package main
import(
	 "git.oschina.net/sblig/pholcus/exec"
		// _ "github.com/pholcus/spider_lib"
	"git.oschina.net/sblig/pholcus/app/downloader/request" //必需
	"git.oschina.net/sblig/pholcus/common/goquery"         //DOM解析
	. "git.oschina.net/sblig/pholcus/app/spider" //必需
	// net包
	//"net/http" //设置http.Header
)

func main(){
	exec.DefaultRun("web")
}


func init() {
	lux.Register()
}

var lux = &Spider{
	Name:        "获取新闻",
	Description: "获取新闻 [Auto Page] [https://xxx.xxxx.org/product/]",
	// Pausetime: 300,
	// Keyin:   KEYIN,
	// Limit:        LIMIT,
	EnableCookie: true,
	RuleTree: &RuleTree{
		Root: func(ctx *Context) {
			ctx.AddQueue(&request.Request{Url: "https://xxx.xxx.org/product/watch/list_bd444_ys442_lx440_pr273_dy445.htm", Rule: "列表页面"})
		},

		Trunk: map[string]*Rule{
			"列表页面": {
				ParseFunc: func(ctx *Context) {
					query := ctx.GetDom()
					query.Find(".lux-filter-list a").Each(func(i int, s *goquery.Selection) {
						if i == 0 {
							return
						}
						if url, ok := s.Attr("href"); ok {
							ctx.AddQueue(&request.Request{
								Url: "https://xxx.xxx.org"+ url, 
								Rule: "列表页面", 
								Temp: map[string]interface{}{"goodsType": s.Text()},
							})
						}
					})
					query.Find(".page a").Each(func(i int, s *goquery.Selection) {
						if i == 0 {
							return
						}
						if url, ok := s.Attr("href"); ok {
							ctx.AddQueue(&request.Request{
								Url: "https://xxx.xxx.org"+ url, 
								Rule: "列表页面", 
								Temp: map[string]interface{}{"goodsType": s.Text()},
							})
						}
					})
				},
			},
			// "筛选页面": {
			// 	ParseFunc: func(ctx *Context) {
			// 		query := ctx.GetDom()
			// 		query.Find(".imgBox").Each(func(i int, s *goquery.Selection) {
			// 			if url, ok := s.Find("a").Attr("href"); ok {
			// 				ctx.AddQueue(&request.Request{
			// 					Url:   "https://xxx.xxx.org"+url,
			// 					Rule: "详情页面",
			// 					Temp: map[string]interface{}{"goodsType": ctx.GetTemp("goodsType", "").(string)},
			// 				})
			// 			}
			// 		})
			// 	},
			// },
			// "详情页面": {
			// 	ParseFunc: func(ctx *Context) {
			// 		// 结果存入Response中转
			// 		ctx.Output(map[string]interface{}{
			// 			"Body":   ctx.GetText(),
			// 			"Cookie": ctx.GetCookie(),
			// 		})
			// 	},
			// },
		},
	},
}